package top.yangbuyi.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

import top.yangbuyi.constants.Constants;
import top.yangbuyi.domain.Dept;
import top.yangbuyi.dto.DeptDto;
import top.yangbuyi.mapper.DeptMapper;
import top.yangbuyi.service.impl.DeptService;
import top.yangbuyi.vo.DataGridView;

import javax.xml.soap.Detail;

/**
 * ClassName: Loginfo
 * Description: 杨不易网站 :www.yangbuyi.top
 * date: 2020/9/3
 *
 * @author TeouBle
 * @author yangbuyi
 * @since JDK 1.8
 **/

@Service
public class DeptServiceImpl implements DeptService {

	  @Autowired
	  private DeptMapper deptMapper;

	  /**
	   * @param deptDto
	   * @Description: 杨不易个人网址:http://yangbuyi.top
	   * 功能描述: 用于分页查询科室数据 查询可用的
	   * @Author: TeouBle
	   * @Date: 2020/9/3 9:56
	   */
	  @Override
	  public DataGridView listPage(DeptDto deptDto) {
			Page<Dept> deptPage = new Page<>(deptDto.getPageNum(), deptDto.getPageSize());
			QueryWrapper<Dept> qw = new QueryWrapper<>();
			qw.like(StringUtils.isNotBlank(deptDto.getDeptName()), Dept.COL_DEPT_NAME, deptDto.getDeptName());
			qw.eq(StringUtils.isNotBlank(deptDto.getStatus()), Dept.COL_STATUS, deptDto.getStatus());
			qw.ge(deptDto.getBeginTime() != null, Dept.COL_CREATE_TIME, deptDto.getBeginTime());
			qw.le(deptDto.getEndTime() != null, Dept.COL_CREATE_TIME, deptDto.getEndTime());

			qw.orderByAsc(Dept.COL_ORDER_NUM);
			this.deptMapper.selectPage(deptPage, qw);
			return new DataGridView(deptPage.getTotal(), deptPage.getRecords());
	  }

	  /**
	   * @Description: 杨不易个人网址:http://yangbuyi.top
	   * 功能描述: 查询所有数据 不分页
	   * @Author: TeouBle
	   * @Date: 2020/9/3 9:57
	   */
	  @Override
	  public List<Dept> listDept() {
			QueryWrapper<Dept> qw = new QueryWrapper<>();
			qw.eq(Dept.COL_STATUS, Constants.STATUS_TRUE);
			qw.orderByAsc(Dept.COL_ORDER_NUM);
			return this.deptMapper.selectList(qw);
	  }

	  /**
	   * @param id
	   * @Description: 杨不易个人网址:http://yangbuyi.top
	   * 功能描述: 根据部门ID 查询信息
	   * @Author: TeouBle
	   * @Date: 2020/9/3 10:02
	   */
	  @Override
	  public Dept getOne(Long id) {
			return this.deptMapper.selectById(id);
	  }

	  /**
	   * @param dept
	   * @Description: 杨不易个人网址:http://yangbuyi.top
	   * 功能描述: 更新部门
	   * @Author: TeouBle
	   * @Date: 2020/9/3 9:59
	   */
	  @Override
	  public int updateDept(DeptDto deptDto) {
			Dept dept = new Dept();
			BeanUtil.copyProperties(deptDto,dept);
			// 设置修改者
			dept.setUpdateBy(deptDto.getSimpleUser().getUserName());
			return this.deptMapper.updateById(dept);
	  }

	  /**
	   * @param dept
	   * @Description: 杨不易个人网址:http://yangbuyi.top
	   * 功能描述: 添加部门
	   * @Author: TeouBle
	   * @Date: 2020/9/3 9:59
	   */
	  @Override
	  public int addDept(DeptDto deptDto) {
			Dept dept = new Dept();
			BeanUtil.copyProperties(deptDto,dept);
			// 创建者、时间
			dept.setUpdateBy(deptDto.getSimpleUser().getUserName());
			dept.setCreateTime(DateUtil.date());
			return this.deptMapper.insert(dept);
	  }

	  /**
	   * @param ids
	   * @Description: 杨不易个人网址:http://yangbuyi.top
	   * 功能描述: 批量删除
	   * @Author: TeouBle
	   * @Date: 2020/9/3 10:01
	   */
	  @Override
	  public int deleteDeptByIds(Long[] ids) {
			if (null != ids && ids.length > 0)
				  return this.deptMapper.deleteBatchIds(Arrays.asList(ids));
			return 0;
	  }

    @Override
    public List<Dept> listDeptByDeptIds(List<Long> deptIds) {
		QueryWrapper<Dept> qw = new QueryWrapper<>();
		qw.in(Dept.COL_DEPT_ID,deptIds);
		return this.deptMapper.selectList(qw);
    }

    @Override
    public void updateDeptRegNumber(Long deptId, Integer regNumber) {
		Dept dept=new Dept();
		dept.setDeptId(deptId);
		dept.setRegNumber(regNumber);
		this.deptMapper.updateById(dept);
    }
}
